package view.table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import entiey.Menu;
import entiey.Table;
import service.TableService;
import service.impl.TableServiceImpl;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JMenuBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;

public class V_table extends JFrame {

	private JPanel contentPane;
	private JPanel panel_1_1;
	private TableService ts=new TableServiceImpl();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_table frame = new V_table();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_table() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 885, 501);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 849, GroupLayout.PREFERRED_SIZE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 442, GroupLayout.PREFERRED_SIZE))
		);
		
		JPanel panel_1_1 = new JPanel();
		tabbedPane.addTab("全部餐桌", null, panel_1_1, null);
		panel_1_1.setLayout(null);
		
		
		//循环=================开始
		JPopupMenu popupMenu = new JPopupMenu();
		
		JMenuItem mntmNewMenuItem1 = new JMenuItem("选择");
		mntmNewMenuItem1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		popupMenu.add(mntmNewMenuItem1);
		JMenuItem mntmNewMenuItem2 = new JMenuItem("取消");
		popupMenu.add(mntmNewMenuItem2);
		int y = 10;
		int x = 10;
		 List<Table> list=ts.findAll();
		Table table=new Table();
		for (int i = 1; i <= list.size(); i++) {
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
			panel_1.setBounds(x, y, 160, 100);
			panel_1_1.add(panel_1);
			panel_1.setLayout(null);
			//设置桌子编号
			int num=list.get(i-1).getDeskNo();
			JLabel label = new JLabel(""+num);
			label.setBounds(10, 10, 54, 15);
			panel_1.add(label);
			//设置桌子名称
			JLabel label_1 = new JLabel(list.get(i-1).getNode());
			label_1.setBounds(10, 35, 54, 15);
			panel_1.add(label_1);
			if(i %5 == 0) {
				y+=100;
				x = 10;
			}else {
				x += 170;
			}
			
			addPopup(panel_1, popupMenu);
		}
		//循环----------------结束
		
		
		
	
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("空餐桌", null, panel_2, null);
		contentPane.setLayout(gl_contentPane);
		
	}

	
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	
	
	
	
	
	
	
	
}
